package com.activeMQ.parkFee;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang.StringUtils;

import com.activeMq.MqSender;
import com.orangelife.exception.AppBizException;
import com.orangelife.utils.ProConfig;

/***
 * 发送缴费信息到刀闸
 * 
 * @author shendiao
 * 
 */
public class ParkFee {

	public static void main(String[] args) throws Exception {

		Map<String, Object> sendPropertyInfosMap = new HashMap<String, Object>();
		sendPropertyInfosMap.put("apartmentID",
				"0004cac0-63f6-4d2f-9efc-06819b451b26");
		sendPropertyInfosMap.put("startTime", "2016-01");
		sendPropertyInfosMap.put("endTime", "2016-09");
		sendPropertyInfosMap.put("payTime", new Date().getTime());
		sendPropertyInfosMap.put("parkFee", "1000.00");

		MqSender.sendTopicMsInfoToService(
				"tcp://admin:admin@10.168.8.101:61616", "PROPFEE.PARKFEE",
				getParkFee(sendPropertyInfosMap), "RECEIVER_EDIT_PARKFEE");

	}

	/***
	 * 发送缴费信息到刀闸
	 * 
	 * @param parkFee
	 *            （ apartmentID 房屋ID startTime 续费开始时间 endTime 续费结束时间 payTime
	 *            缴费时间 缴费金额parkFee）
	 * @throws AppBizException
	 */
	public static void sendPayFeeToParkSys(Map<String, Object> parkFee)
			throws AppBizException {
		MqSender.sendTopicMsInfoToService(ProConfig.getProValues("ACTMQURL"),
				"PROPFEE.PARKFEE", getParkFee(parkFee), "RECEIVER_EDIT_PARKFEE");

	}

	public static Map<String, Object> getParkFee(Map<String, Object> parkFee)
			throws AppBizException {

		Map<String, Object> paMap = new HashMap<String, Object>();

		if (!parkFee.containsKey("apartmentID")) {
			throw new AppBizException(2, "亲，请填写车位信息");

		} else {
			if (parkFee.get("apartmentID") == null) {
				new AppBizException(2, "亲，请填写车位信息");

			}
		}

		if (!parkFee.containsKey("startTime")) {
			new AppBizException(2, "亲，请填写车位续费开始时间");

		} else {
			if (parkFee.get("startTime") == null) {
				new AppBizException(2, "亲，请填写车位续费开始时间");

			}
		}

		if (!parkFee.containsKey("endTime")) {
			new AppBizException(2, "亲，请填写车位续费结束时间");
		} else {
			if (parkFee.get("endTime") == null) {
				new AppBizException(2, "亲，请填写车位续费结束时间");

			}
		}

		if (!parkFee.containsKey("payTime")) {
			new AppBizException(2, "亲，请填写车位缴费时间");
		} else {
			if (parkFee.get("payTime") == null) {
				new AppBizException(2, "亲，请填写车位缴费时间");

			}
		}

		if (!parkFee.containsKey("parkFee")) {
			new AppBizException(2, "亲，请填写车位缴费金额");
		} else {
			if (parkFee.get("parkFee") == null) {
				new AppBizException(2, "亲，请填写车位缴费金额");
			}
		}

		if (!StringUtils.isBlank(parkFee.get("startTime").toString())) {
			if (!matchDate(parkFee.get("startTime").toString())) {
				new AppBizException(2, "亲，请填写正确车位续费开始时间");
			}
		}
		if (!StringUtils.isBlank(parkFee.get("endTime").toString())) {
			if (!matchDate(parkFee.get("endTime").toString())) {
				new AppBizException(2, "亲，请填写正确车位续费结束时间");
			}
		}

		Date startTime = getTime(parkFee.get("startTime").toString());
		Date endTime = getLastMonthDay(parkFee.get("endTime").toString());

		if (startTime.getTime() > endTime.getTime()) {
			new AppBizException(2, "亲，续费开始时间大于续费结束时间");
		}
		paMap.put("parkID", parkFee.get("apartmentID"));
		paMap.put("startTime", startTime.getTime());
		paMap.put("endTime", endTime.getTime());
		paMap.put("payTime", parkFee.get("payTime"));
		paMap.put("parkFee", parkFee.get("parkFee"));
		return paMap;

		// SimpleDateFormat format = newSimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		// String time="1970-01-06 11:45:55";
		// Date date = format.parse(time);
	}

	/***
	 * 判断是否为yyyy-mm 格式
	 * 
	 * @param time
	 * @return
	 */
	public static boolean matchDate(String time) {
		Pattern pattern = Pattern.compile("(\\d){4}-(\\d){2}");
		Matcher matcher = pattern.matcher(time);
		if (matcher.find()) {
			return true;
		} else {
			return false;
		}

	}

	/**
	 * 将yyyy-mm 转时间获取当月最后一天
	 * 
	 * @param time
	 * @return
	 * @throws ParseException
	 */
	public static Date getLastMonthDay(String time) {
		// SimpleDateFormat format1 = new
		// SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Calendar calendar = Calendar.getInstance();// 日历对象
		try {
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
			Date date = format.parse(time);

			calendar.setTime(date);// 设置当前日期
			calendar.add(Calendar.MONTH, 1);// 月份减一
			// System.out.println(sdf.format(calendar.getTime()));// 输出格式化的日期
			calendar.setTime(calendar.getTime());
			calendar.add(Calendar.SECOND, -1);// 天数减一
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return calendar.getTime();

	}

	/***
	 * 将yyyy-mm 转时间
	 * 
	 * @param time
	 * @return
	 */
	public static Date getTime(String time) {
		// SimpleDateFormat format1 = new
		// SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Calendar calendar = Calendar.getInstance();// 日历对象
		try {
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
			Date date = format.parse(time);
			calendar.setTime(date);// 设置当前日期
			// System.out.println(format1.format(calendar.getTime()));//
			// 输出格式化的日期
		} catch (Exception e) {
			// TODO: handle exception
		}
		return calendar.getTime();

	}

}
